---
title: "Help Doc: kubectl expose"
categories:
- devops
- kubectl
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org7d335bc">Usage:</a></li>
<li><a href="#orgb9902b2">Examples:</a></li>
<li><a href="#org9c56adc">Options:</a>
<ul>
<li><a href="#orgdf7b679">–allow-missing-template-keys=true</a></li>
<li><a href="#org2b29fe8">–cluster-ip=''</a></li>
<li><a href="#org97d3379">–dry-run=false</a></li>
<li><a href="#orga2720f9">–external-ip=''</a></li>
<li><a href="#org5944aaa">-f, –filename=[]</a></li>
<li><a href="#org07ffb6a">–generator='service/v2'</a></li>
<li><a href="#orge6eae29">-k, –kustomize=''</a></li>
<li><a href="#org6578089">-l, –labels=''</a></li>
<li><a href="#org90b5d3b">–load-balancer-ip=''</a></li>
<li><a href="#org2ad499b">–name=''</a></li>
<li><a href="#orge8a88db">-o, –output=''</a></li>
<li><a href="#org1d972c5">–overrides=''</a></li>
<li><a href="#org79e912e">–port=''</a></li>
<li><a href="#org2529e30">–protocol=''</a></li>
<li><a href="#orgf5dddc0">–record=false</a></li>
<li><a href="#org1929613">-R, –recursive=false</a></li>
<li><a href="#orgea93f9d">–save-config=false</a></li>
<li><a href="#org8db4ce0">–selector=''</a></li>
<li><a href="#org4bc3b9c">–session-affinity=''</a></li>
<li><a href="#orgea66e67">–target-port=''</a></li>
<li><a href="#org1f04750">–template=''</a></li>
<li><a href="#org632d8c4">–type=''</a></li>
</ul>
</li>
</ul>
</div>
</div>
<p>
Expose a resource as a new Kubernetes service.
</p>
<p>
Looks up a deployment, service, replica set, replication controller or pod by name and uses the selector for that resource as the selector for a new service on the specified port. 
A deployment or replica set will be exposed as a service only if its selector is convertible to a selector that service supports, i.e. when the selector contains only the matchLabels component. 
Note that if no port is specified via –port and the exposed resource has multiple ports, all will be re-used by the new service. 
Also if no labels are specified, the new service will re-use the labels from the resource it exposes.
</p>
<p>
Possible resources include (case insensitive):
</p>
<ul class="org-ul">
<li>pod (po),</li>
<li>service (svc),</li>
<li>replicationcontroller (rc),</li>
<li>deployment (deploy),</li>
<li>replicaset (rs)</li>
</ul>
<div class="outline-2" id="outline-container-org7d335bc">
<h2 id="org7d335bc">Usage:</h2>
<div class="outline-text-2" id="text-org7d335bc">
<p>
kubectl expose (-f FILENAME | TYPE NAME) [–port=port] [–protocol=TCP|UDP|SCTP] [–target-port=number-or-name]
[–name=name] [–external-ip=external-ip-of-service] [–type=type] [options]
</p>
<p>
Use "kubectl options" for a list of global command-line options (applies to all commands).
</p>
</div>
</div>
<div class="outline-2" id="outline-container-orgb9902b2">
<h2 id="orgb9902b2">Examples:</h2>
<div class="outline-text-2" id="text-orgb9902b2">
<p>
kubectl expose rc nginx –port=80 –target-port=8000
</p>
<p>
kubectl expose -f nginx-controller.yaml –port=80 –target-port=8000
</p>
<p>
kubectl expose pod valid-pod –port=444 –name=frontend
</p>
<p>
kubectl expose service nginx –port=443 –target-port=8443 –name=nginx-https
</p>
<p>
kubectl expose rc streamer –port=4100 –protocol=UDP –name=video-stream
</p>
<p>
kubectl expose rs nginx –port=80 –target-port=8000
</p>
<p>
kubectl expose deployment nginx –port=80 –target-port=8000
</p>
</div>
</div>
<div class="outline-2" id="outline-container-org9c56adc">
<h2 id="org9c56adc">Options:</h2>
<div class="outline-text-2" id="text-org9c56adc">
</div>
<div class="outline-3" id="outline-container-orgdf7b679">
<h3 id="orgdf7b679">–allow-missing-template-keys=true</h3>
<div class="outline-text-3" id="text-orgdf7b679">
<p>
If true, ignore any errors in templates when a field or map key is missing in the template. 
Only applies to golang and jsonpath output formats.
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org2b29fe8">
<h3 id="org2b29fe8">–cluster-ip=''</h3>
<div class="outline-text-3" id="text-org2b29fe8">
<p>
ClusterIP to be assigned to the service. Leave empty to auto-allocate, or set to 'None' to create a headless service.
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org97d3379">
<h3 id="org97d3379">–dry-run=false</h3>
<div class="outline-text-3" id="text-org97d3379">
<p>
If true, only print the object that would be sent, without sending it.
</p>
</div>
</div>
<div class="outline-3" id="outline-container-orga2720f9">
<h3 id="orga2720f9">–external-ip=''</h3>
<div class="outline-text-3" id="text-orga2720f9">
<p>
Additional external IP address (not managed by Kubernetes) to accept for the service. 
If this IP is routed to a node, the service can be accessed by this IP in addition to its generated service IP.
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org5944aaa">
<h3 id="org5944aaa">-f, –filename=[]</h3>
<div class="outline-text-3" id="text-org5944aaa">
<p>
Filename, directory, or URL to files identifying the resource to expose a service
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org07ffb6a">
<h3 id="org07ffb6a">–generator='service/v2'</h3>
<div class="outline-text-3" id="text-org07ffb6a">
<p>
The name of the API generator to use. There are 2 generators: 'service/v1' and 'service/v2'. 
The only difference between them is that service port in v1 is named 'default', while it is left unnamed in v2. Default is 'service/v2'.
</p>
</div>
</div>
<div class="outline-3" id="outline-container-orge6eae29">
<h3 id="orge6eae29">-k, –kustomize=''</h3>
<div class="outline-text-3" id="text-orge6eae29">
<p>
Process the kustomization directory. This flag can't be used together with -f or -R.
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org6578089">
<h3 id="org6578089">-l, –labels=''</h3>
<div class="outline-text-3" id="text-org6578089">
<p>
Labels to apply to the service created by this call.
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org90b5d3b">
<h3 id="org90b5d3b">–load-balancer-ip=''</h3>
<div class="outline-text-3" id="text-org90b5d3b">
<p>
IP to assign to the LoadBalancer. If empty, an ephemeral IP will be created and used (cloud-provider specific).
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org2ad499b">
<h3 id="org2ad499b">–name=''</h3>
<div class="outline-text-3" id="text-org2ad499b">
<p>
The name for the newly created object.
</p>
</div>
</div>
<div class="outline-3" id="outline-container-orge8a88db">
<h3 id="orge8a88db">-o, –output=''</h3>
<div class="outline-text-3" id="text-orge8a88db">
<p>
Output format. One of:
json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file.
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org1d972c5">
<h3 id="org1d972c5">–overrides=''</h3>
<div class="outline-text-3" id="text-org1d972c5">
<p>
An inline JSON override for the generated object. If this is non-empty, it is used to override the generated object. 
Requires that the object supply a valid apiVersion field.
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org79e912e">
<h3 id="org79e912e">–port=''</h3>
<div class="outline-text-3" id="text-org79e912e">
<p>
The port that the service should serve on. Copied from the resource being exposed, if unspecified
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org2529e30">
<h3 id="org2529e30">–protocol=''</h3>
<div class="outline-text-3" id="text-org2529e30">
<p>
The network protocol for the service to be created. Default is 'TCP'.
</p>
</div>
</div>
<div class="outline-3" id="outline-container-orgf5dddc0">
<h3 id="orgf5dddc0">–record=false</h3>
<div class="outline-text-3" id="text-orgf5dddc0">
<p>
Record current kubectl command in the resource annotation. If set to false, do not record the command. 
If set to true, record the command. If not set, default to updating the existing annotation value only if one exists.
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org1929613">
<h3 id="org1929613">-R, –recursive=false</h3>
<div class="outline-text-3" id="text-org1929613">
<p>
Process the directory used in -f, –filename recursively. Useful when you want to manage related manifests organized within the same directory.
</p>
</div>
</div>
<div class="outline-3" id="outline-container-orgea93f9d">
<h3 id="orgea93f9d">–save-config=false</h3>
<div class="outline-text-3" id="text-orgea93f9d">
<p>
If true, the configuration of current object will be saved in its annotation. Otherwise, the annotation will be unchanged. 
This flag is useful when you want to perform kubectl apply on this object in the future.
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org8db4ce0">
<h3 id="org8db4ce0">–selector=''</h3>
<div class="outline-text-3" id="text-org8db4ce0">
<p>
A label selector to use for this service. Only equality-based selector requirements are supported.
If empty (the default) infer the selector from the replication controller or replica set.)
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org4bc3b9c">
<h3 id="org4bc3b9c">–session-affinity=''</h3>
<div class="outline-text-3" id="text-org4bc3b9c">
<p>
If non-empty, set the session affinity for the service to this; legal values: 'None', 'ClientIP'
</p>
</div>
</div>
<div class="outline-3" id="outline-container-orgea66e67">
<h3 id="orgea66e67">–target-port=''</h3>
<div class="outline-text-3" id="text-orgea66e67">
<p>
Name or number for the port on the container that the service should direct traffic to.
Optional.
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org1f04750">
<h3 id="org1f04750">–template=''</h3>
<div class="outline-text-3" id="text-org1f04750">
<p>
Template string or path to template file to use when -o=go-template, -o=go-template-file. 
The template format is golang templates [<a href="http://golang.org/pkg/text/template/#pkg-overview">http://golang.org/pkg/text/template/#pkg-overview</a>].
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org632d8c4">
<h3 id="org632d8c4">–type=''</h3>
<div class="outline-text-3" id="text-org632d8c4">
<p>
Type for this service: ClusterIP, NodePort, LoadBalancer, or ExternalName. Default is 'ClusterIP'.
</p>
</div>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="author">Author: gdme1320</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
